package com.example.work;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
public class register extends AppCompatActivity {
    EditText user, pwo, rpw, uname;
    Button buttonR, buttonB;
    CheckBox checkBox3;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        DBHelper1 dbHelper = new DBHelper1(register.this,"test.db",null,1);
        // 获取数据库的可写实例
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor =  db.query("userInfo",new String[]{"user"},"user=?",new String[]{String.valueOf(user)},null,null,null);
        if(cursor.moveToFirst()) {
            // 如果结果不为空，则表示用户名已存在，并且显示 "用户名已存在" 的 Toast 消息
            Toast.makeText(register.this,"用户名已存在",Toast.LENGTH_SHORT).show();
        } else {
            // 如果结果为空，则表示用户名不存在，并且执行 SQL 插入语句，将用户名和密码插入数据库
            String SQLstr = "insert into userInfo(user,pwo) values(?,?)";
            db.execSQL(SQLstr, new String[]{String.valueOf(user), String.valueOf(pwo)});

            // 显示 "注册成功" 的 Toast 消息
            Toast.makeText(register.this, "注册成功", Toast.LENGTH_SHORT).show();
        }
        setContentView(R.layout.activity_register);
        {
            buttonB = findViewById(R.id.buttonB);
            buttonR = findViewById(R.id.buttonR);
            uname = findViewById(R.id.uname);
            pwo = findViewById(R.id.pwo);
            rpw = findViewById(R.id.rpw);
            user = findViewById(R.id.user);
            checkBox3 = findViewById(R.id.checkBox3);
            buttonR.setOnClickListener(new View.OnClickListener()
            {
                @Override

                public void onClick(View view)
                {
                    if (checkBox3.isChecked())
                    {
                        if (rpw.getText().toString().trim().equals("") || uname.getText().toString().trim().equals("") || pwo.getText().toString().trim().equals("") || user.getText().toString().trim().equals(""))
                        {
                            Toast.makeText(register.this, "请填写所有内容", Toast.LENGTH_LONG).show();
                            return;
                        }
                        if (!rpw.getText().toString().trim().equals(pwo.getText().toString().trim()))
                        {
                            Toast.makeText(register.this, "两次密码输入不一致，请重新输入", Toast.LENGTH_LONG).show();
                            return;
                        }
                        String res = "欢迎你，你的注册信息如下：\n" + "用户名：" + user.getText().toString().trim() + "\n密码：" + pwo.getText().toString().trim() + "\n姓名：" + uname.getText().toString().trim() + "\n性别" + "\n";
                        Toast.makeText(register.this, res, Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(register.this, "请阅读会员协议并勾选", Toast.LENGTH_LONG).show();
                    }

                }
            });
            //点击返回跳转登录界面
            buttonB = findViewById(R.id.buttonB);
            buttonB.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {

                    finish();
                }
            });
        }

    }
}